---
category: Random
contributors:
    - robinpokorny
created: '2020-04-19'
title: Generate a random hex color
updated: '2021-10-13'
---

**JavaScript version**

```js randomColor.js
const randomColor = () => `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`;

// Or
const randomColor = () => `#${(~~(Math.random() * (1 << 24))).toString(16)}`;
```

**TypeScript version**

```ts randomColor.ts
const randomColor = (): string => `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`;

// Or
const randomColor = (): string => `#${(~~(Math.random() * (1 << 24))).toString(16)}`;
```
